package system_storage_service

import (
	"math/rand"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/service/system_storage_service/provider"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/service/system_storage_service/provider/minio"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/service/system_storage_service/provider/s3v1"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/service/system_storage_service/provider/s3v2"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/configuration"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/dao"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/service"
)

func New(object dao.SystemObject) *Storage {
	return &Storage{
		BaseService: service.New(object),
		object:      object,
	}
}

type Storage struct {
	*service.BaseService
	object dao.SystemObject
}

func (cls *Storage) GetProvider() (provider.Provider, errors.Error) {
	switch configuration.Configuration.Storage.ProviderPolicy {
	case "first":
		for _, providerConfig := range configuration.Configuration.Storage.Providers {
			if providerConfig.Enable {
				switch providerConfig.Protocol {
				case "s3v1":
					p, err := s3v1.New(providerConfig)
					if err != nil {
						return nil, err.Relation(errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.362308", "创建S3V1供应商失败"))
					}

					return p, nil
				case "s3v2":
					p, err := s3v2.New(providerConfig)
					if err != nil {
						return nil, err.Relation(errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.432308", "创建S3V2供应商失败"))
					}

					return p, nil
				case "minio":
					p, err := minio.New(providerConfig)
					if err != nil {
						return nil, err.Relation(errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.422243", "创建Minio供应商失败"))
					}

					return p, nil
				}

				return nil, errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.332752", "存储协议无效")
			}
		}

		return nil, errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.332752", "没有激活的存储供应商")
	case "random":
		var indexes []int
		for index, providerConfig := range configuration.Configuration.Storage.Providers {
			if providerConfig.Enable {
				indexes = append(indexes, index)
			}
		}

		if len(indexes) == 0 {
			return nil, errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.332752", "没有激活的存储供应商")
		}

		var randomIndex = indexes[rand.Intn(len(indexes))]
		var providerConfig = configuration.Configuration.Storage.Providers[randomIndex]

		switch providerConfig.Protocol {
		case "s3v1":
			p, err := s3v1.New(providerConfig)
			if err != nil {
				return nil, err.Relation(errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.802309", "创建S3V1供应商失败"))
			}

			return p, nil
		case "s3v2":
			p, err := s3v2.New(providerConfig)
			if err != nil {
				return nil, err.Relation(errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.643107", "创建S3V2供应商失败"))
			}

			return p, nil
		case "minio":
			p, err := minio.New(providerConfig)
			if err != nil {
				return nil, err.Relation(errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.792243", "创建Minio供应商失败"))
			}

			return p, nil
		}

		return nil, errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.332752", "存储协议无效")
	}

	return nil, errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.282525", "存储策略无效")
}

func (cls *Storage) GetProviderByName(name string) (provider.Provider, errors.Error) {
	for _, providerConfig := range configuration.Configuration.Storage.Providers {
		if providerConfig.Name == name {
			switch providerConfig.Protocol {
			case "s3v1":
				p, err := s3v1.New(providerConfig)
				if err != nil {
					return nil, err.Relation(errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.1132309", "创建S3V1供应商失败"))
				}

				return p, nil
			case "s3v2":
				p, err := s3v2.New(providerConfig)
				if err != nil {
					return nil, err.Relation(errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.835656", "创建S3V2供应商失败"))
				}

				return p, nil
			case "minio":
				p, err := minio.New(providerConfig)
				if err != nil {
					return nil, err.Relation(errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.1052244", "创建Minio供应商失败"))
				}

				return p, nil
			}

			return nil, errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.895703", "存储协议无效")
		}
	}

	return nil, errors.New(errors.ErrorTypeServerInternalError, "ISS_GE.S_GE.GP_ER.935708", "存储供应商不存在")
}
